from flask import Flask, request, jsonify
from utils.login import check_code, login

app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello, World!'

@app.route('/<name>', methods=['GET'])
def hello_world_with_name(name):
    return 'Hello,'+ name + '!'

@app.route('/hello', methods=['POST'])
def hello():
    try:
        data = request.get_json()

        name = data.get('name')
        age = data.get('age')

        if name is None or age is None:
            return jsonify({"error": "Name and age are required"}), 400

        response_text = 'Hello, {}! Your age is {}'.format(name, age)

        return jsonify({"message": response_text})

    except Exception as e:
        return jsonify({"error": str(e)}), 500

@app.route('/code', methods=['POST'])
def code():
    try:
        data = request.get_json()
        code_url = data.get('url')
        check_code_key, check_code_num = check_code(code_url)
        return jsonify({"check_code_key": check_code_key, "check_code_num": check_code_num})

    except Exception as e:
        return str(e)


@app.route('/login', methods=['GET'])
def log_in():
    try:
        auth = login()
        if auth is None:
            return jsonify({"error": "authorization is null"}), 400
        return jsonify({"authorization": auth})
    except Exception as e:
        return str(e)

if __name__ == '__main__':
    app.run(host='0.0.0.0',  debug=True)
